const { task,start,series,src, dest,watch } = require('gulp');
const babel = require('gulp-babel');
const uglify = require('gulp-uglify');
const rename = require('gulp-rename');
const del = require('delete');
const imagemin = require('gulp-imagemin');
const pngquant = require('imagemin-pngquant');
const cache = require('gulp-cache');
const gulpSequence = require('gulp-sequence');
//sprite 图
const spritesmith = require('gulp.spritesmith');
//md5 插件 
const rev= require('gulp-rev');
//html 替换css,js语言件

const revCollector = require('gulp-rev-collector');
const runSequence = require('run-sequence');

//css压缩
const minifyCss = require('gulp-minify-css');

const minifyHtml = require("gulp-minify-html");

const transport = require("gulp-seajs-transport");
const concat = require("gulp-seajs-concat");
const chsiRev = require('gulp-chsi-rev');
const browserSync = require('browser-sync').create();

function sprite() {
    del("build/icons/*");
    return src("src/icons/*.{png,jpg,gif,ico}")
        .pipe(spritesmith({
            imgName: `icons.png`,
            cssName: 'icons.css',
            algorithm: 'binary-tree',
            padding: 6,
            cssVarMap: function(sprite) {
                sprite.name = sprite.name;
            }
        }))
        
        .pipe(dest("build/icons/"))
        
}




function spritePatchCss() {
    
    return src(["build/icons/*.css"])
        .pipe(rev())
        .pipe(dest('build/icons/'));
        // .pipe(rev.manifest())
        // .pipe(dest('build/rev-iconsCss'));
}

function spritePatchPng() {
    return src(["build/icons/*.png"])
        .pipe(rev())
        .pipe(dest('build/icons/'))
        // .pipe(rev.manifest())
        // .pipe(dest('build/rev-iconsPng'));
}
 
// css
function css(cb) {
    if (process.env.NODE_ENV == 'production') {
        return src('src/css/*.css')
        .pipe(minifyCss())
        // .pipe(rev())
        .pipe(dest('build/css/'))
        // .pipe(rev.manifest())                       //- 生成一个rev-manifest.json
        // .pipe(dest('build/rev-css/')) 
    } else {
        src('src/css/*.css')
        .pipe(dest('build/css/'))
    }
    cb();
}


function img(cb) {
    return src("src/imgs/*.{png,jpg,gif,ico}")
        .pipe(cache(imagemin({
            progressive: true,
            svgoPlugins: [{removeViewBox: false}],//不要移除svg的viewbox属性
            use: [pngquant()] //使用pngquant深度压缩png图片的imagemin插件
        })))
        // .pipe(rev())
        .pipe(dest("build/imgs/"))
        // .pipe(rev.manifest())                   //- 生成一个rev-manifest.json
        // .pipe(dest('build/rev-imgs/')) 
    cb();
}

function js() {
    // body omitted
    // del(['build/js/*.js','build/**/*.js.map']);
    if (process.env.NODE_ENV == 'production') {
        return src('src/js/*.js',{ sourcemaps: true })
        .pipe(babel())
        .pipe(chsiRev())
        // .pipe(rev())
        .pipe(transport({
        	idleading:"/js/"
        }))
        .pipe(concat({
            base: "/js/"
        }))
        .pipe(uglify({
            mangle:{
                reserved: ['require', 'exports', 'module','define','sea', 'seajs', 'sea.js', '$', 'Zepto', 'jQuery', '_hmt'] //这几个变量不压缩
            }
        }))
       
        
        .pipe(dest('build/js/',{ sourcemaps: '.'}))
        // .pipe(rev.manifest())                       //- 生成一个rev-manifest.json
        // .pipe(dest('build/rev-js/'))                 //- 将rev-manifest.json保存到rev-css目录内
    
    } else {
        return src('src/js/*.js',{ sourcemaps: true })
        // .pipe(transport({
        // 	idleading:"/js/"
        // }))
        // .pipe(concat())
        .pipe(babel())
        // .pipe(rev())
        .pipe(dest('build/js/',{ sourcemaps: '.'}));
    }

  }

  function html () {
        del.sync('build/*.html');
        if (process.env.NODE_ENV == 'production') {
        return src(['build/**/rev-*.json', 'src/*.html', 'src/js/*.js'])
            // .pipe(revCollector({
            //     replaceReved: true
            // }))

            .pipe(chsiRev())
            .pipe(minifyHtml())
            .pipe(dest('build/'));
        } else {
            return src(['build/**/rev-*.json', 'src/*.html'])
            .pipe(revCollector({
                replaceReved: true
            }))
            .pipe(dest('build/'));
        }
        
  }

  function clean() {
    return del('build/*')
    
  }
  
  function transportFun(cb) {
    src('src/js/*.js')
        .pipe(transport())
        .pipe(dest('temp/'))
    cb();
  }



  exports.ts = transportFun;

  function libs () {
    return src('src/libs/*.js')
        .pipe(dest('build/libs/'));
  }

// 静态服务器
 function browserSyncFn() {
    browserSync.init({
        server: {
            baseDir: "./build/"
        }
    });
}


exports.default = series(clean,js,css,libs,img,series(sprite, spritePatchCss, spritePatchPng),html, browserSyncFn)

watch('src/js/*.js', series([js],[html]));
watch('src/css/*.css', series(css,html));
watch('src/imgs/*.{png,jpg,gif,ico}',img);
watch('src/icons/*.{png,jpg,gif,ico}',series(sprite, spritePatchCss, spritePatchPng));
watch('src/*.{html,htm}', html);
watch('src/libs/*.js', libs);

